//
//  ElectrombileViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/17.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "ElectrombileViewController.h"
#import "EvDetailsViewController.h"
#import "DeviceModel.h"
@interface ElectrombileViewController ()
@property(nonatomic,assign)NSInteger acType;
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation ElectrombileViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getCarList];
}
-(void)getCarList
{
    NSString *URLString = @"/axq/admin/device/list";
    NSDictionary *parameters = @{@"status": @"success"};
    [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        self.tableView.className=@"EvmCell";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSString *code=[NetworkPath getValue:@"code" from:responseObject];
        //上面方法都会有字符串 所以用length 判断
        if (code.length) {
            if([code isEqualToString:@"10008"])
            {
                if ([NetworkPath isNSArray:@"data" from:responseObject]) {
                    NSArray *data=responseObject[@"data"];
                    for (int i=0; i<data.count; i++) {
                        if ([data[i] isKindOfClass:[NSDictionary class]]) {
                            DeviceModel *model=[DeviceModel modelWithDict:data[i]];
                            EvmData *dataObj=[[EvmData alloc]init];
                            if (![NSString isNULLString:model.img1]) {
                                dataObj.image=model.img1;
                            }else
                            {
                                if (![NSString isNULLString:model.img2]) {
                                    dataObj.image=model.img2;
                                }else
                                {
                                    if (![NSString isNULLString:model.img3]) {
                                        dataObj.image=model.img3;
                                    }else
                                    {
                                        dataObj.image=@"";
                                    }
                                }
                            }
                            dataObj.name=model.devicename;
                            dataObj.storeName=model.shopname;
                            dataObj.brand=model.brandname;
                            dataObj.color=model.color;
                            dataObj.state=model.status;
                            dataObj.model=model;
                            [itemArr addObject:dataObj];
                        }
                    }
                }
            }else
            {
                
                NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                
                [self.view makeToast:str];
            }
        }
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView reloadData];
    } RequestFailed:^(NSError *error) {
        self.tableView.className=@"EvmCell";
        NSMutableArray *itemArr=[NSMutableArray array];
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView reloadData];
    }];
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor=[UIColor colorF];
    self.navigationController.navigationBar.barTintColor = [UIColor colorQ];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    
    
    
    
    
    self.navigationItem.title = @"电动车管理";
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    
    
    
//    UIButton *rightBtn=[NetworkPath SetNavRightBtnWithController:self withTitle:@"扫一扫" andImageName:nil];
//    rightBtn.selected=YES;
//    [rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    
    
//    UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
//
//
//    searchBar.placeholder=@"查询车架号或电机号";
//    searchBar.backgroundImage=[NetworkPath createImageWithColor:[UIColor colorF]];
//
//    [[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
//    UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"];
//    [txfSearchField setDefaultTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.5]}];
//
//    searchBar.searchTextPositionAdjustment = UIOffsetMake(7, 0);
    
    
    self.tableView =[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-BOTTOM_HEIGHT)  withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        EvmCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        EvDetailsViewController *vc=[[EvDetailsViewController alloc]init];
        vc.deviceid=cell.dataDic.model.deviceid;
        NSLog(@"状态%@",cell.dataDic.state);
        if ([cell.dataDic.state isEqualToString:@"1"]) {
            vc.type=@"未审核";
        }else if ([cell.dataDic.state isEqualToString:@"3"])
        {
            vc.type=@"已审核";
        }else
        {
           vc.type=@"未通过";
        }
//        switch (self.acType) {
//            case 0:
//                vc.type=@"未审核";
//                break;
//            case 1:
//                vc.type=@"已审核";
//                break;
//            case 2:
//                vc.type=@"未通过";
//                break;
//
//            default:
//                break;
//        }
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    
    [self.view addSubview:self.tableView];
    
    
    
    
    
//    UISegmentedControl *TypeSegmentedControl = [[UISegmentedControl alloc] initWithItems:
//                                                [NSArray arrayWithObjects:
//                                                 @"未审核",
//                                                 @"已审核",
//                                                 @"未通过",
//                                                 nil]];
//    TypeSegmentedControl.apportionsSegmentWidthsByContent = YES;
//    TypeSegmentedControl.layer.masksToBounds=YES;
//    TypeSegmentedControl.layer.cornerRadius=5;
//    TypeSegmentedControl.tintColor = [UIColor colorF];
//    NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor color6],NSForegroundColorAttributeName,[UIFont systemFontOfSize:12.0],NSFontAttributeName,nil];
//    [TypeSegmentedControl setTitleTextAttributes:dics forState:UIControlStateNormal];
//    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor colorQ]]
//                                    forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
//    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor whiteColor]]
//                                    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
//
//    [TypeSegmentedControl addTarget:self action:@selector(TypeAction:) forControlEvents:UIControlEventValueChanged];
//    [self.view addSubview:TypeSegmentedControl];
//    [TypeSegmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(10);
//        make.top.mas_equalTo(TOP_HEIGHT+10);
//        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-20, 30));
//
//    }];
//
//
//    TypeSegmentedControl.selectedSegmentIndex=0;
//    [self TypeAction:TypeSegmentedControl];
    
    
    
}
//调用方法
- (UIImage*)searchFieldBackgroundImage {
    UIColor*color = [UIColor whiteColor];
    CGFloat cornerRadius = 5;
    CGRect rect =CGRectMake(0,0,28,28);
    
    UIBezierPath*roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    roundedRect.lineWidth=0;
    
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    [color setFill];
    [roundedRect fill];
    [roundedRect stroke];
    [roundedRect addClip];
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (void)TypeAction:(UISegmentedControl *)segmentedControl
{
    self.acType = segmentedControl.selectedSegmentIndex;
    switch (self.acType) {
        case 0:
        {
            
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @""};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                //1.先判断responseObject是不是字典
                if ([responseObject isKindOfClass:[NSDictionary class]]) {
                    //2.再判断字典有没有code key
                    if (responseObject[@"code"]) {
                        //3. code转化为字符串 防止类型崩溃
                        NSString *code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                        if([code isEqualToString:@"10008"]){
                            //4.再判断有没有data key
                            if (responseObject[@"data"]) {
                                //5.再判断data 是不是字典（数组）
                                if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
                                    NSArray *data=responseObject[@"data"];
                                    //6.判断数组有没有数据
                                    if (data.count) {
                                        for (int i=0; i<data.count; i++) {
                                            //7.判断是不是字典
                                            if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                                NSDictionary *dic=data[i];
                                                DeviceModel *model=[DeviceModel modelWithDict:dic];
                                                
                                                EvmData *dataObj=[[EvmData alloc]init];
                                                if (![NSString isNULLString:model.img1]) {
                                                    dataObj.image=model.img1;
                                                }else
                                                {
                                                    if (![NSString isNULLString:model.img2]) {
                                                        dataObj.image=model.img2;
                                                    }else
                                                    {
                                                        if (![NSString isNULLString:model.img3]) {
                                                            dataObj.image=model.img3;
                                                        }else
                                                        {
                                                            dataObj.image=@"";
                                                        }
                                                    }
                                                }
                                                dataObj.name=model.devicename;
                                                dataObj.storeName=model.shopname;
                                                dataObj.brand=model.brandname;
                                                dataObj.color=model.color;
                                                dataObj.state=model.status;
                                                dataObj.model=model;
                                                [itemArr addObject:dataObj];
                                                
                                                
                                                
                                                
                                                
                                            }
                                        }
                                    }
                                }
                                
                            }
                            
                        }
                        else
                        {
                            NSString *str=responseObject[@"msg"];
                            
                            [self.view makeToast:str];
                        }
                        
                        
                        
                    }
                }
                
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
                
                
                
                
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
        }
            break;
        case 1:
        {
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @"success"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code=[NetworkPath getValue:@"code" from:responseObject];
                //上面方法都会有字符串 所以用length 判断
                if (code.length) {
                    if([code isEqualToString:@"10008"])
                    {
                        if ([NetworkPath isNSArray:@"data" from:responseObject]) {
                            NSArray *data=responseObject[@"data"];
                            for (int i=0; i<data.count; i++) {
                                if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                    DeviceModel *model=[DeviceModel modelWithDict:data[i]];
                                    EvmData *dataObj=[[EvmData alloc]init];
                                    if (![NSString isNULLString:model.img1]) {
                                        dataObj.image=model.img1;
                                    }else
                                    {
                                        if (![NSString isNULLString:model.img2]) {
                                            dataObj.image=model.img2;
                                        }else
                                        {
                                            if (![NSString isNULLString:model.img3]) {
                                                dataObj.image=model.img3;
                                            }else
                                            {
                                                dataObj.image=@"";
                                            }
                                        }
                                    }
                                    dataObj.name=model.devicename;
                                    dataObj.storeName=model.shopname;
                                    dataObj.brand=model.brandname;
                                    dataObj.color=model.color;
                                    dataObj.state=model.status;
                                    dataObj.model=model;
                                    [itemArr addObject:dataObj];
                                }
                            }
                        }
                    }else
                    {
                        
                        NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                        
                        [self.view makeToast:str];
                    }
                }
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
        }
            break;
        case 2:
        {
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @"failed"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code=[NetworkPath getValue:@"code" from:responseObject];
                //上面方法都会有字符串 所以用length 判断
                if (code.length) {
                    if([code isEqualToString:@"10008"])
                    {
                        if ([NetworkPath isNSArray:@"data" from:responseObject]) {
                            NSArray *data=responseObject[@"data"];
                            for (int i=0; i<data.count; i++) {
                                if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                    DeviceModel *model=[DeviceModel modelWithDict:data[i]];
                                    EvmData *dataObj=[[EvmData alloc]init];
                                    if (![NSString isNULLString:model.img1]) {
                                        dataObj.image=model.img1;
                                    }else
                                    {
                                        if (![NSString isNULLString:model.img2]) {
                                            dataObj.image=model.img2;
                                        }else
                                        {
                                            if (![NSString isNULLString:model.img3]) {
                                                dataObj.image=model.img3;
                                            }else
                                            {
                                                dataObj.image=@"";
                                            }
                                        }
                                    }
                                    dataObj.name=model.devicename;
                                    dataObj.storeName=model.shopname;
                                    dataObj.brand=model.brandname;
                                    dataObj.color=model.color;
                                    dataObj.state=model.status;
                                    dataObj.model=model;
                                    [itemArr addObject:dataObj];
                                }
                            }
                        }
                    }else
                    {
                        
                        NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                        
                        [self.view makeToast:str];
                    }
                }
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
        }
            break;
            
            
        default:
            break;
    }
}
//-(void)rightAction
//{
//    [self.view makeToast:@"开发中"];
//}



@end
